package com.zsyh.changshang.adress;

import android.app.Dialog;
import android.app.DialogFragment;
import android.app.FragmentManager;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.zsyh.changshang.R;
import com.zsyh.changshang.basic.MyAppliaction;
import com.zsyh.changshang.util.SPUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 项目名称:xm
 * 创建人:liangwanbo
 * 创建时间:2018/7/27 10:29
 */
public class TestAdressPop extends DialogFragment implements AddressCallBack {

    private SelectAddresFinish mSelectAddresFinish;
    private Context context;
    private View view;
    private ViewPager viewPager;
    private PagerSlidingTabStrip pagerTab;
    private FrameLayout popBg;

    private AddressManager.Province province;
    private AddressManager.City city;
    private AddressManager.District district;
    private String defutText;
    private ProvinceFragment mProvinceFragment;
    private CityFragment mCityFragment;
    private DistrictFragment mDistrictFragment;
    private LinearLayout contentView;
    private String[] address = new String[]{"省份", "城市", "地区"};

    private Button queding;
    private TextView tvTitle;
    private String title;

    public void setSelectAddresFinish(SelectAddresFinish mSelectAddresFinish) {
        this.mSelectAddresFinish = mSelectAddresFinish;
    }


    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        context = getActivity();
        initView();
        Dialog dialog = new Dialog(context, R.style.CustomDatePickerDialog);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); // must be called before set content
        dialog.setContentView(view);
        dialog.setCanceledOnTouchOutside(true);

        // 设置宽度为屏宽、靠近屏幕底部。
        Window window = dialog.getWindow();
        WindowManager.LayoutParams wlp = window.getAttributes();
        wlp.gravity = Gravity.BOTTOM;
        wlp.width = WindowManager.LayoutParams.MATCH_PARENT;
        window.setAttributes(wlp);
        return dialog;
    }


    public void setAddress(String pCode, String cCode, String aCode) {
        if (StringUtils.isNoEmpty(pCode) && StringUtils.isNoEmpty(cCode) && StringUtils.isNoEmpty(aCode)) {
            province = AddressManager.newInstance().findProvinceByCode(pCode);
           // city = province.findCityByCode(cCode);
            // district = city.findDistrictByCode(aCode);
        }
    }

    public void setExpress(String express) {
        this.title = express;
    }

    private void initView() {
        view = LayoutInflater.from(context).inflate(R.layout.select_address_pop_layout, null);
        tvTitle = view.findViewById(R.id.titles);
        if ("express".equals(title)){
            tvTitle.setText("请选择收货地址");
        }
        ImageView ivClose = (ImageView) view.findViewById(R.id.ivClose);
        contentView = (LinearLayout) view.findViewById(R.id.contentView);
        viewPager = (ViewPager) view.findViewById(R.id.viewPager);
        pagerTab = (PagerSlidingTabStrip) view.findViewById(R.id.pagerTab);
        popBg = (FrameLayout) view.findViewById(R.id.popBg);
        queding = (Button) view.findViewById(R.id.queding);
        queding.setVisibility(View.GONE);
        //defutText = context.getString(R.string.selset_birthday);
        pagerTab.setTextSize(DensityUtils.sp2px(context, 14));
        pagerTab.setSelectedColor(getResources().getColor(R.color.regis_account_exist));
        pagerTab.setTextColor(getResources().getColor(R.color.qianse));

        List<View> lis = new ArrayList<View>();
        mProvinceFragment = new ProvinceFragment(context, this);
        mCityFragment = new CityFragment(context, this);
        mDistrictFragment = new DistrictFragment(context, this);
        lis.add(mProvinceFragment.getListview());
        lis.add(mCityFragment.getListview());
        lis.add(mDistrictFragment.getListview());
        viewPager.setAdapter(new AddressListAdapter(lis));

        String[] addres = null;
        if (province != null && city != null) {
            addres = new String[]{province.getName(), city.getName()};

            mProvinceFragment.setCode(province.getCode());
            mCityFragment.setCode(province.getCode(), city.getCode());
            Log.e("tempcode",province.getCode());
            mDistrictFragment.setCode(province.getCode(),city.getCode(),"");
            viewPager.setCurrentItem(1);
            pagerTab.setTabsText(address);
            pagerTab.setCurrentPosition(1);
        } else {

            addres = new String[]{defutText};
            viewPager.setCurrentItem(0);
            pagerTab.setTabsText(address);
            pagerTab.setCurrentPosition(0);
        }
        ivClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
        popBg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
        pagerTab.setTabOnClickListener(new TabOnClickListener() {
            @Override
            public void onClick(View tab, int position) {
               /* if (defutText.equals(pagerTab.getTabs()[position])) {

                }*/
              /*  viewPager.setCurrentItem(position);
                pagerTab.setCurrentPosition(position);*/
                String[] addres = null;
                switch (position) {
                    case 0:
                        viewPager.setCurrentItem(0);
                        pagerTab.setCurrentPosition(0);
                        if (district != null) {
                           // addres = new String[]{province.getName(), city.getName(), district.getName()};
                        } else if (city != null) {
                           // addres = new String[]{province.getName(), city.getName(), defutText};
                        } else {
                           // addres = new String[]{province.getName(), defutText};
                        }
                        break;
                    case 1:
                        viewPager.setCurrentItem(1);
                        pagerTab.setCurrentPosition(1);
                       /* if (district != null) {
                            addres = new String[]{province.getName(), city.getName(),district.getName()};
                        }else {
                            addres = new String[]{province.getName(), city.getName(), defutText};
                        }*/
                        //mDistrictFragment.resetIndex();
                        break;
                    case 2:
                        viewPager.setCurrentItem(2);
                        pagerTab.setCurrentPosition(2);
                        //addres = new String[]{province.getName(),city.getName(),district.getName()};
                        break;

                    default:
                }
                pagerTab.setTabsText(address);
                pagerTab.setCurrentPosition(position);
            }
        });

        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                pagerTab.setCurrentPosition(i);
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });

    }

    @Override
    public void show(FragmentManager manager, String tag) {
        //KeyBoardUtils.closeKeybord((Activity) context);
        super.show(manager, tag);
    }

    @Override
    public void selectProvince(AddressManager.Province province) {
        String[] addres = new String[]{province.getName(), defutText};
        pagerTab.setTabsText(address);
        pagerTab.setCurrentPosition(1);
        viewPager.setCurrentItem(1);
        if (province != this.province) {
            city = null;
            district = null;
        }
        this.province = province;
        //mCityFragment.setCode(province.getCode(),null);
        SPUtils.put(MyAppliaction.getContext(),"cityshuju",province.getCode());
        mCityFragment.setCode(province.getCode(), province.getName());
    }

    @Override
    public void selectCity(AddressManager.City city) {
        String[] addres = new String[]{province.getName(), city.getName(), defutText};
        pagerTab.setTabsText(address);
        pagerTab.setCurrentPosition(2);
        viewPager.setCurrentItem(2);
        if (city != this.city) {
            district = null;
        }

        this.city = city;
        mDistrictFragment.setCode(province.getCode(), city.getCode(), null);
        SPUtils.put(MyAppliaction.getContext(),"areashuju",city.getCode());
        Log.e("citycodess",city.getCode());
        //mSelectAddresFinish.finish(province.getCode(), city.getCode(), province.getName() + city.getName());
        //dismiss();
    }

    @Override
    public void selectDistrict(AddressManager.District district) {
        //String[] addres = new String[]{province.getName(), city.getName(), district.getName()};
        pagerTab.setTabsText(address);
        this.district = district;
        mSelectAddresFinish.finish(province.getCode(), city.getCode(), district.getCode(),province.getName(),city.getName(),district.getName());
        Log.e("qudai",district.getCode()+"iskong");
        dismiss();
    }
}
